// pages/filter/content/content.js

var utils = require("../../../utils/utils.js");
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    datainfo: [],
    screenType: "",
    screenName: "",
    filterArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var content = app.globalData.screenContent;

    var filterData = utils.filterToArr(app.globalData.searchInfo.ST_FILTERFIELD);
    this.data.filterArr = filterData;

    var value = "";  //选中的值
    for (var item in filterData) {
      var key = filterData[item].key;
      if (key == content.ST_CODE) {
        value = filterData[item].value;
        break;
      }
    }

    var items = [];
    for (var item in content.LT_VALUES) {
      var val = content.LT_VALUES[item];
      items.push({
        screenName: val,
        screenSelected: val == value
      });
    }

    this.setData({
      datainfo: items,
      screenType: content.ST_TYPE,
      screenName: content.ST_CODE
    });
  },

  changeTap: function (event) {
    var key = this.data.screenName;
    var value = event.currentTarget.dataset.name;
    var iseq = false;  //key，value 和原来的是否相同
    if (key && value && key.length > 0 && value.length > 0) {
      var filterData = this.data.filterArr;
      var isext = false;
      for (var item in filterData) {
        var okey = filterData[item].key;
        if (key == okey) {
          iseq = filterData[item].value == value;
          filterData[item].value = value;
          isext = true;
          break;
        }
      }
      if (!isext) {
        filterData.push({
          key: key,
          value: value
        });
      }
      //当选择分类时，清除其他类别
      if (key == "st_web_catalog") {
        filterData = [];
        filterData.push({
          key: key,
          value: value
        });
      }

      //点击返回事件
      app.globalData.screenContent.isck = true;
      app.globalData.screenContent.iseq = iseq;

      app.globalData.searchInfo.ST_FILTERFIELD = utils.filterToStr(filterData);
    }
    wx.navigateBack({
      delta: 1
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})